import Http from "./http";
import { imgUrls } from '@/utils/utils';

/**
 * 发送验证码
 * @param {string} phone - 手机号码
 * @returns {Promise<boolean>} - 发送结果
 * @throws {Error} - 当请求失败时抛出错误
 */
export const sendCode = async (phone) => {
  try {
    const response = await Http.post({
      url: '/admin/sendCode',
      data: { phone }
    });

    return response.code === 200;
  } catch (error) {
    console.error('发送验证码失败:', error);
    throw new Error('发送验证码失败');
  }
};

/**
 * 导出账号列表
 * @param {Object} params - 查询参数
 * @returns {Promise<string>} - 导出链接
 * @throws {Error} - 当请求失败时抛出错误
 */
export const exportUserList = async (params) => {
  try {
    const res = await Http.get({
      url: '/yp_admin/downloadUserList',
      params
    });
    if (res.data) {
        const downloadUrl = imgUrls(res.data);
        const link = document.createElement('a');
        link.href = downloadUrl;
        link.download = '账号列表.xlsx';
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        ElMessage.success('导出成功');
      } else {
        ElMessage.warning('未找到可下载的文件');
      }
  } catch (error) {
    console.error('导出账号列表失败:', error);
    throw new Error('导出账号列表失败');
  }
};


export const exportRegistrationForm = async (params) => {
  try {
    const res = await Http.post({
      url: '/yp_admin/exportRegistrationForm',
      data:params
    });
    if (res.data) {
        const downloadUrl = imgUrls(res.data.url);
        const link = document.createElement('a');
        link.href = downloadUrl;
        link.download = res.data.fileName;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        ElMessage.success('导出成功');
      } else {
        ElMessage.warning('未找到可下载的文件');
      }
  } catch (error) {
    console.error('导出账号列表失败:', error);
    throw new Error('导出账号列表失败');
  }
};


// 导出展演活动列表
export const exportActivityList = async (params) => {
  try {
    const res = await Http.get({
      url: '/yp_admin/exportActivityList',
      params
    });
    if (res.data) {
        const downloadUrl = imgUrls(res.data);
   
        const link = document.createElement('a');
        link.href = downloadUrl;
        link.download = res.data.fileName;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        ElMessage.success('导出成功');
      } else {
        ElMessage.warning('未找到可下载的文件');
      }
  } catch (error) {
    console.error('导出展演活动列表失败:', error);
    throw new Error('导出展演活动列表失败');
  }
};

// 删除展演报名
export const deleteRegistration = async (id) => {
  try {
    const res = await Http.post({
      url: '/yp_admin/deleteRegistration',
      data:{
        id
      }
    });
    return res.code === 200;
  } catch (error) {
    console.error('删除展演报名失败:', error);
    throw new Error('删除展演报名失败');
  }
};


// 导出展演活动列表
export const exportRegistrationList = async (params) => {
  try {
    const res = await Http.get({
      url: '/yp_admin/exportActivityRegistrationList',
      params
    });
    if (res.data) {
        const downloadUrl = imgUrls(res.data);
    
        const link = document.createElement('a');
        link.href = downloadUrl;
        link.download = res.data.fileName;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        ElMessage.success('导出成功');
      } else {
        ElMessage.warning('未找到可下载的文件');
      }
  } catch (error) {
    console.error('导出展演活动列表失败:', error);
    throw new Error('导出展演活动列表失败');
  }
};